package com.jxsd.edu.cn.tools;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

public class MailManager
{
	private static  String   hostName="smtp.163.com";
	private static String    userFromEmail="wfysea@163.com";
	private static String    password="wfysea163";
	private static String    userFromName="wfysea";
	//获取一个简单邮件对象
   public  static SimpleEmail  getSimpleEmail(String toEmail,String toName,String  url)
   {
	   SimpleEmail  simple=new SimpleEmail();
	   simple.setCharset("gb2312");
	   configEmail(hostName,userFromEmail,userFromName,password,toEmail,toName,simple);
	   subMgs(simple,toEmail,url);
	   return  simple;
   }
   //获取一个HtmlEmail邮件对象
   public static  HtmlEmail  getHtmlEmail(String toEmail,String toName,String  url)
   {
	   HtmlEmail htmlemail=new HtmlEmail();
	   configEmail(hostName,userFromEmail,userFromName,password,toEmail,toName,htmlemail);
	   subMgs(htmlemail,toEmail,url);
	   return  htmlemail;
   }
   
   //获取一个附件邮件对象
   public  static MultiPartEmail  getMultiPartEmail(String toEmail,String toName,String  url)
   {
	   MultiPartEmail  multipart=new  MultiPartEmail();
	   configEmail(hostName,userFromEmail,userFromName,password,toEmail,toName,multipart);
	   subMgs(multipart,toEmail,url);
	   return  multipart;
   }
   
   //配置邮件发送主题和消息
   private static  void  subMgs(Email email,String  mailto,String  url)
   {
	    email.setSubject(getsubject());
	    try {
			email.setMsg(getMsg(mailto,url));
		} catch (EmailException e) {
			e.printStackTrace();
		}
   }
   
   
   
   //配置邮件发送参数
   private static  void  configEmail(String hostName,String userFromEmail,String userFromName,
		                     String password,String toEmail,String toName,Email email)
   {
	   //设置发送邮箱的主机名
	     email.setHostName(hostName);
	     System.out.println("主机名:"+hostName);
       try {
    	 //设置收件人邮箱 
		 email.addTo(toEmail,toName);
		 System.out.println("收件人邮箱:"+toEmail);
		 System.out.println("收件人昵称:"+toName);
		//发件人邮箱 
		 email.setFrom(userFromEmail, userFromName);
		 System.out.println("发件人邮箱:"+userFromEmail);
		 System.out.println("发件人昵称:"+userFromName);
	        } catch (EmailException e) {
		          e.printStackTrace();
	          } 
       //如果要求身份验证，设置用户名、密码，分别为发件人在邮件服务器上注册的用户名和密码 
       email.setAuthentication(userFromName, password); 
       System.out.println("发件人邮箱登陆密码:"+password);
   }
   
  
 //获取邮件的主题(注册主题)
   private static String  getsubject()
   {
	   String  subject=null;
	   subject="恭喜注册师大淘吧成功!";
	   return  subject;
   }
   
   //获取邮件的消息内容(注册内容)
   private  static String  getMsg(String  mailto,String url)
   {
	   String  message=null;
	   StringBuilder  s=new StringBuilder();
	   s.append("师大淘吧通行证");
	   s.append("\n");
	   s.append("亲爱的");
	   s.append(mailto);
	   s.append(":\n");
	   s.append("感谢您申请注册师大淘吧通行证!请点击链接完成注册:\n");
	   s.append(url);
	   s.append("\n如果你点击无效,请将这个URL复制到浏览器的地址栏中打开\n如果您没有申请师大淘吧通行证,请忽略此邮件!\n");
	   message=s.toString();
	   return  message;
   }
   
    //获取URL
 	public static String  getURL(String  userId,String  authcode)
 	{
 		String  userID=userId;
 		String  url=null;
 		StringBuilder  s=new  StringBuilder();
 		String  basePath="http://localhost:8080";
 		s.append(basePath);
 		String  projectPath="/jxsdmarket";
 		s.append(projectPath);
 		s.append("/servlet/MailRegister.jsp");
 		s.append("?action=register");
 		s.append("&loginName=");
 		s.append(userID);
 		s.append("&authCode=");
 		s.append(authcode);
 		url=s.toString();
 	    return url;
 	}
}
